{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc3a52b0-14b3-4fa0-b2f1-97ac7935ce3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    "# author : Liu Kun\n",
    "# date   : 2021-08-27 23:46:55\n",
    "\n",
    "import os\n",
    "import yaml\n",
    "import pymysql\n",
    "\n",
    "\n",
    "class Config(object):\n",
    "\n",
    "    __debug = False\n",
    "    __mode = None\n",
    "    __filename = None\n",
    "    __yaml = None\n",
    "    __host = \"127.0.0.1\"\n",
    "    __port = 3306\n",
    "    __username = \"root\"\n",
    "    __password = \"123456\"\n",
    "    __database = \"secret\"\n",
    "    __table = \"config\"\n",
    "\n",
    "    def __init__(self, filename=None, host=None, port=None, username=None, password=None, debug=True):\n",
    "        if filename is None:\n",
    "            self.__construct_database(host, port, username, password, debug)\n",
    "        elif None in [host, port, username, password]:\n",
    "            self.__construct_yaml(filename, debug)\n",
    "        else:\n",
    "            pass\n",
    "        pass\n",
    "\n",
    "    def __construct_database(self, host, port, username, password, debug=True):\n",
    "        self.__mode = \"database\"\n",
    "        self.__host = host\n",
    "        self.__port = port\n",
    "        self.__username = username\n",
    "        self.__password = password\n",
    "        self.__debug = debug\n",
    "        pass\n",
    "\n",
    "    def __construct_yaml(self, filename, debug=True):\n",
    "        self.__mode = \"yaml\"\n",
    "        self.__filename = filename\n",
    "        self.__debug = debug\n",
    "        self.__load()\n",
    "        pass\n",
    "\n",
    "    def __load(self):\n",
    "        if not os.access(self.__filename, os.F_OK):\n",
    "            print(f'The config file {self.__filename} not exist!!!')\n",
    "            return None\n",
    "        with open(self.__filename, 'r') as f:\n",
    "            self.__yaml = yaml.load(f, Loader=yaml.FullLoader)\n",
    "            if self.__debug:\n",
    "                print(self.__yaml)\n",
    "        pass\n",
    "\n",
    "    def get_database(self, tag):\n",
    "        if self.__mode == \"yaml\":\n",
    "            return self.__get_conf_from_yaml('database', tag)\n",
    "        elif self.__mode == \"database\":\n",
    "            return self.__get_conf_from_database('database', tag)\n",
    "        else:\n",
    "            return None\n",
    "\n",
    "    def get_object_storage(self, tag):\n",
    "        if self.__mode == \"yaml\":\n",
    "            return self.__get_conf_from_yaml('object-storage', tag)\n",
    "        elif self.__mode == \"database\":\n",
    "            return self.__get_conf_from_database('object-storage', tag)\n",
    "        else:\n",
    "            return None\n",
    "\n",
    "    def __get_conf_from_database(self, node, tag):\n",
    "        db = pymysql.connect(host=self.__host, port=self.__port, user=self.__username, password=self.__password, database=self.__database)\n",
    "        cursor = db.cursor()\n",
    "        params = None\n",
    "        try:\n",
    "            sql = f\"SELECT params FROM `{self.__table}` WHERE `node` = '{node}' AND `tag` = '{tag}'\"\n",
    "            cursor.execute(sql)\n",
    "            result = cursor.fetchone()\n",
    "            if result is not None and len(result) == 1:\n",
    "                params = result[0]\n",
    "        except Exception:\n",
    "            print(\"ERROR: \", sql)\n",
    "        db.close()\n",
    "        return params\n",
    "\n",
    "    def __get_conf_from_yaml(self, node, tag):\n",
    "        if node not in self.__yaml:\n",
    "            print(f'Can not find {node} in yaml!!!')\n",
    "            return None\n",
    "        for e in self.__yaml[node]:\n",
    "            if tag not in e.keys():\n",
    "                continue\n",
    "            return e[tag]\n",
    "        print(f'Can not find {node} tag={tag} in yaml!!!')\n",
    "        return None\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    inst = Config(filename='../resources/config.yaml')\n",
    "    # inst = Config(host='127.0.0.1', port=3306, username='root', password='123456')\n",
    "    print('-' * 100)\n",
    "    print(inst.get_database('ds218-plus-test'))\n",
    "    print(inst.get_object_storage('ds218-plus-test'))\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a82d6a71-4c86-4fc3-a147-c1e041df4d93",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
